package com.gouda.core.service.impl.serviceIntroduce;

import com.google.common.base.Strings;
import com.gouda.core.dao.ServiceIntroduceDao;
import com.gouda.core.dao.ServiceTypeDao;
import com.gouda.core.dto.serviceIntroduce.ServiceIntroduceDto;
import com.gouda.core.entity.ServiceIntroduceEntity;
import com.gouda.core.entity.ServiceTypeEntity;
import com.gouda.core.service.serviceIntroduce.ServiceIntroduceService;
import org.amethyst.atlas.basic.manufactory.specifics.DBEntityFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by souu on 15/12/6.
 */
@Service
public class ServiceIntroduceServiceImpl implements ServiceIntroduceService {

    @Autowired
    private ServiceTypeDao serviceTypeDao;

    @Autowired
    private ServiceIntroduceDao serviceIntroduceDao;

    @Override
    public void createServiceIntroduce(long serviceId, double price, int startHour,
                                       int space, String spaceRange, String serviceContent, String safeServiceContent) {
        if (serviceId <= 0 || price < 0 || startHour <= 0 || space <= 0
                || Strings.isNullOrEmpty(spaceRange) || Strings.isNullOrEmpty(serviceContent)
                || Strings.isNullOrEmpty(safeServiceContent)) {
            return;
        }
        ServiceTypeEntity serviceType = this.serviceTypeDao.unique(ServiceTypeEntity.class, serviceId);
        if (serviceType == null) {
            return;
        }
        ServiceIntroduceEntity entity = DBEntityFactory.getFactory().generateDBEntity(ServiceIntroduceEntity.class);
        entity.setCreateTime(new Date());
        entity.setId(serviceId);
        entity.setPrice((int) (price * 100));
        entity.setSafeServiceContent(safeServiceContent);
        entity.setServiceContent(serviceContent);
        entity.setSpace(space);
        entity.setSpaceRange(spaceRange);
        entity.setStartHour(startHour);
        entity.setServiceName(serviceType.getServiceName());
        this.serviceIntroduceDao.insert(entity);

    }

    @Override
    public void updateServiceIntroduce(long serviceId, double price, int startHour,
                                       int space, String spaceRange, String serviceContent, String safeServiceContent) {
        ServiceIntroduceEntity entity = this.serviceIntroduceDao.unique(ServiceIntroduceEntity.class, serviceId);
        if (entity == null) {
            return;
        }
        entity.setCreateTime(new Date());
        entity.setId(serviceId);
        entity.setPrice((int) (price * 100));
        entity.setSafeServiceContent(safeServiceContent);
        entity.setServiceContent(serviceContent);
        entity.setSpace(space);
        entity.setSpaceRange(spaceRange);
        entity.setStartHour(startHour);
        this.serviceIntroduceDao.insert(entity);
    }

    @Override
    public ServiceIntroduceDto getServiceIntroduceById(long serviceId) {
        ServiceIntroduceEntity entity = this.serviceIntroduceDao.unique(ServiceIntroduceEntity.class, serviceId);
        if (entity == null) {
            return null;
        }
        ServiceIntroduceDto dto = new ServiceIntroduceDto();
        dto.setCreateTime(entity.getCreateTime());
        dto.setId(entity.getId());
        dto.setPrice(entity.getPrice() / 100);
        dto.setSafeServiceContent(entity.getSafeServiceContent());
        dto.setServiceContent(entity.getServiceContent());
        dto.setSpace(entity.getSpace());
        dto.setSpaceRange(entity.getSpaceRange());
        dto.setStartHour(entity.getStartHour());
        dto.setServiceName(entity.getServiceName());
        return dto;
    }
}
